package htlgkr.nodlehs;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SubjectList extends ListActivity{
	
	SQLiteDatabase db;
	Hashtable<String, String> subsID;
	ArrayList<String> subs = new ArrayList<String>();
	String menheader = "";
	ArrayAdapter<String> subjectAdapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subjectlist);
		NodlehsDatabase bridge = new NodlehsDatabase(this);
		db = bridge.getWritableDatabase();
		subsID = new Hashtable<String, String>();
		loadSub();
		}
	
	public void loadSub() {
		Cursor subjects = db.query("subjects", DBSubjectTbl.ALL_COLUMNS,
				"_id>?", new String[] { "0" }, null, null, null);

		if(!(subjects.getCount() <= 0)){
			while (subjects.moveToNext()) {
			subsID.put(subjects.getString(1), subjects.getString(0));
			subs.add(subjects.getString(1));
		}}
		if (subs.isEmpty()) {
			subs.add("Keine Elemente vorhanden");
		}
		subjectAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, subs);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		setListAdapter(subjectAdapter);

	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.detail:
			Intent i = new Intent(getApplicationContext(),
					CreateNewSubjectActivity.class);
			i.putExtra("SUBID", getSubID(menheader));
			startActivityForResult(i, 222);
			return true;
		case R.id.delete:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private int getSubID(String string) {
		Cursor subList = db.query("subjects", DBSubjectTbl.ALL_COLUMNS, "_id>?",
				new String[] { "0" }, null, null, null);
		int id = 0;
		while (subList.moveToNext()) {
				if (subList.getString(1).equals(string)) {
					id = Integer.parseInt(subList.getString(0));
					
					
					break;
				}
			}
		Log.i("SubID", id+"");
		return id;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.subjectmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.men_newSub:
			Intent i = new Intent(getApplicationContext(),
					CreateNewSubjectActivity.class);
			i.putExtra("SubjectID", 0);
			startActivityForResult(i, 222);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (resultCode) {
		case 222:
			subjectAdapter.clear();
			loadSub();
		}

	}

}
